package cn.faifai.desktop.events.listener;

import cn.faifai.desktop.events.StageCloseEvent;
import cn.faifai.desktop.utils.StageManager;
import cn.faifai.desktop.view.MainView;
import com.google.common.eventbus.Subscribe;
import javafx.application.Platform;
import javafx.stage.Stage;
import javafx.stage.WindowEvent;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class StageCloseListener {

    public StageCloseListener() {
    }

    @Subscribe
    public void onStageCloseEvent(StageCloseEvent closeEventevent) {
        log.info("收到关闭窗口请求");
//        MainView.getInstance().getRoot().getScene().getWindow().hide();
        String stageKey = closeEventevent.getStageKey();
        Stage stage = StageManager.getInstance().getStage(stageKey);
        if (stageKey.equals("MainStage")){
            // 设置关闭请求申请逻辑
            closeEventevent.getWindowEvent().consume(); // 阻止默认关闭窗口事件
            stage.setIconified(true); // 最小化窗口
            Platform.setImplicitExit(false);// 关闭"没有窗口就退出程序"的默认行为
            stage.hide(); // 隐藏窗口
        }else {
            StageManager.getInstance().closeStage(stageKey);
        }
    }
}
